package net.adetech.dcweb.core.dc.entity;

import net.sourceforge.pinyin4j.PinyinHelper;

import java.util.Comparator;

public class DcInfoPinyinComparator implements Comparator<DcInfo>
{

    @Override
    public int compare(DcInfo info1, DcInfo info2) {

        String realName1 = info1.getRealName();
        String realName2 =info2.getRealName();
        String pinyin1 = getHanyunpinyinString(realName1);
        String pinyin2 = getHanyunpinyinString(realName2);

        return pinyin1.compareTo(pinyin2);
    }


    private String getHanyunpinyinString(String input)
    {
        if (input==null)
        {
            return "";
        }
        StringBuffer outputBuffer = new StringBuffer();
        for (int i = 0;i<input.length();i++)
        {
            char c = input.charAt(i);
            String[] pinyinArray = PinyinHelper.toHanyuPinyinStringArray(c);
            if (pinyinArray==null||pinyinArray.length<1)
            {
                continue;
            }
            String pinyinString = pinyinArray[0];
            if (pinyinString!=null&&pinyinString.length()>0)
            {
                outputBuffer.append(pinyinString.substring(0,pinyinString.length()-1));
            }
        }
        return outputBuffer.toString();
    }
}